﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using DoubleClick.Windows;

namespace DoubleClick
{
    class Program
    {
        private static Configuration config = new Configuration();
        public static IConfiguration Default
        {
            get { return config; }
        }

        public IConfiguration Configuration
        {
            get { return config; }
        }

        public bool ParseArguments(string[] args)
        {
            if (args.Length == 1)
            {
                config.StartupMode = StartupModeEnum.Interactive;
                config.CommandToRun = args[0];
                return false;
            }

            if (args.Length >= 2)
            {
                if (args[0].ToLower() == "service")
                {
                    config.StartupMode = StartupModeEnum.Service;
                    config.CommandToRun = args[1];
                    return false;
                }
                if (args[0].ToLower() == "install")
                {
                    try
                    {
                        config.StartupMode = StartupModeEnum.Service;
                        config.ServiceName = args[1];
                        config.CommandToRun = args[2];
                        new Service().Installer();
                    }
                    catch (Exception) { }
                }
                if (args[0].ToLower() == "uninstall")
                {
                    try
                    {
                        config.StartupMode = StartupModeEnum.Service;
                        config.ServiceName = args[1];
                        new Service().Uninstaller();
                    }
                    catch (Exception) { }
                }
            }
            return true;
        }

        public void StartupAsApplication()
        {
            Server srv = new Server(config);
            srv.Start();

            while (true)
                System.Threading.Thread.Sleep(5000);
        }

        public void StartupAsService()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
                {
                     new Service()
                };
            ServiceBase.Run(ServicesToRun);
        }

        public string GetHelpMessage()
        {
            string text;
            using (StreamReader reader = File.OpenText(@"Help.txt"))
                text = reader.ReadToEnd();
            return text;
        }

        static void Main(string[] args)
        {
            Program pgm = new Program();
            if (pgm.ParseArguments(args))
            {
                System.Windows.Forms.MessageBox.Show(pgm.GetHelpMessage(), "DoubleClick");
                Environment.Exit(0);
            }

            switch (Program.Default.StartupMode)
            {
                case StartupModeEnum.Interactive:
                    pgm.StartupAsApplication();
                    break;
                case StartupModeEnum.Service:
                    pgm.StartupAsService();
                    break;
            }
        }
    }
}
